app.controller('adminCtrl', function($scope, $http,$location, backendSrv,$interval){
    $scope.msg = "";
    $scope.proxy = $scope;

    $scope.clear = function(){
        init();
    }

    $scope.listOrgs = function(){
        backendSrv.cmd('/admin/cmd', {Cmd:'listOrgs'}, 
                function(data){$scope.orgs = data;});
    }
    $scope.listUsers = function(){
        backendSrv.cmd('/admin/cmd', {Cmd:'listUsers'}, 
                function(data){$scope.users = data;});
    }



    $scope.saveOrg = function(org){
        $scope.msg = "Org saved";
        $interval(function(){$scope.msg=""}, 3000, 1 )
        console.log("saveOrg called");
        backendSrv.cmd('/admin/cmd', {Cmd:'saveOrg', Org:org}, 
            function(data){});
    }

    
    $scope.initOrgsUsers = function(){
        $scope.listUsers();
        $scope.listOrgs();
    }


    $scope.addOrg = function(){
        $scope.orgs.push({})
    }


    // events
    $scope.execFilter = function(){
        if($scope.useFilter){
            $scope.$broadcast('filterOrg', {field: $scope.filterField, re: new RegExp($scope.filter, "i") }) 
        }else{
            $scope.$broadcast('filterOrg') 
        }
    }
       
    $scope.openAllOrgs = function(){ $scope.$broadcast('openAllOrgs') }
});
